private["_savedTime","_savedWeather","_overcast","_rain","_fog","_wind","_gusts","_lightnings","_waves","_timeforecast","_timeforecastMinutes","_environmentCreated"];

if (!isServer) exitWith {};

_environmentCreated = profileNamespace getVariable ["AfterlifeEnvironmentCreated", false];

//Make sure server has enviornment created
if !(_environmentCreated) then
{
	profileNamespace setVariable ["AfterlifeEnvironmentCreated", true];
	saveProfileNamespace;
	
	format ["createEnvironment:%1", AfterlifeServerID] call ExileServer_system_database_query_insertSingle;
};

_savedTime = format ["getDate:%1", AfterlifeServerID] call ExileServer_system_database_query_selectSingleField;
_savedWeather = format ["getWeather:%1", AfterlifeServerID] call ExileServer_system_database_query_selectSingleField;

if (typeName _savedTime isEqualTo "ARRAY") then
{
	format ["Loading last saved date : %1", _savedTime] call AfterlifeServer_util_log;
	setDate _savedTime;
}
else
{
	setDate [2016, 9, 14, 06, 0];
	"Loading default date..." call AfterlifeServer_util_log;
};

if (typeName _savedWeather isEqualTo "ARRAY") then
{
	format["Loading last saved weather : %1", _savedWeather] call AfterlifeServer_util_log;
	
	_overcast = _savedWeather select 0;
	_rain = _savedWeather select 1;
	_fog = _savedWeather select 2;
	_wind = _savedWeather select 3;
	_gusts = _savedWeather select 4;
	_lightnings = _savedWeather select 5;
	_waves = _savedWeather select 6;
	
	0 setovercast _overcast;
	0 setrain _rain;
	0 setfog _fog;
	setwind _wind;
	0 setgusts _gusts;
	0 setlightnings _lightnings;
	0 setwaves _waves;
	forceWeatherChange;
}
else
{
	"Loading default weather..." call AfterlifeServer_util_log;
	
	0 setovercast 0;
	0 setrain 0;
	0 setfog [0,0,0];
	setwind [0,0,false];
	0 setgusts 0;
	0 setlightnings 0;
	0 setwaves 0;
	forceWeatherChange;
};

[] spawn {

	"Loading weather controlling system..." call AfterlifeServer_util_log;

	while {true} do {
		call AfterlifeServer_environment_control;

		_timeforecast = 600 + (random 3000);

		_timeforecastMinutes = [_timeforecast,"HH:MM:SS"] call BIS_fnc_secondsToString;
		format ["将会在 %1 后执行环境控制",_timeforecastMinutes] call AfterlifeServer_util_log;
		uiSleep _timeforecast;
	};
};

//Kill Lights
call AfterlifeServer_environment_killLights;

//Spawn Loot
//call AfterlifeServer_environment_spawnLoot;

//Spawn Crash Sites
call AfterlifeServer_environment_spawnCrashSite;

//Singal Everyone
AfterlifeServerEnvironmentInitalized = true;
publicVariable "AfterlifeServerEnvironmentInitalized";